import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'
/**
 * 静态路由表（默认路由）
 * 不需要权限就能访问的路由表
 */
export const staticRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  }, // 首页
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/Home/index'),
        meta: { title: '首页', icon: 'el-icon-s-home' }
      }
    ]
  }
]

/**
 * 动态路由表
 * 需要权限才能访问的路由表
 */
export const allAsyncRoutes = [
  {
    path: '/permission',
    component: Layout,
    redirect: '/permission/user',
    name: 'Permission',
    meta: { title: '权限管理', icon: 'el-icon-setting' },
    children: [
      {
        path: 'user',
        name: 'User',
        component: () => import('@/views/Permission/User/index'),
        meta: { title: '用户管理', icon: 'el-icon-user-solid' }
      },
      {
        path: 'role',
        name: 'Role',
        component: () => import('@/views/Permission/Role/index'),
        meta: { title: '角色管理', icon: 'el-icon-s-custom' }
      },
      {
        path: 'menu',
        name: 'Menu',
        component: () => import('@/views/Permission/Menu/index'),
        meta: { title: '菜单管理', icon: 'el-icon-s-order' }
      },
      {
        path: 'role/auth',
        name: 'RoleAuth',
        component: () => import('@/views/Permission/Role/components/role-auth'),
        hidden: true
      }
    ]
  },

  {
    path: '/product',
    component: Layout,
    redirect: '/product/assort',
    name: 'Product',
    meta: { title: '商品管理', icon: 'el-icon-s-shop' },
    children: [
      {
        path: 'assort',
        name: 'Assort',
        component: () => import('@/views/Commodity/Assort/index'),
        meta: { title: '分类管理', icon: 'el-icon-box' }
      },
      {
        path: 'brand',
        name: 'Brand',
        component: () => import('@/views/Commodity/Brand/index'),
        meta: { title: '品牌管理', icon: 'el-icon-medal' }
      },
      {
        path: 'attribute',
        name: 'Attribute',
        component: () => import('@/views/Commodity/Attribute/index'),
        meta: { title: '平台属性管理', icon: 'el-icon-s-flag' }
      },
      {
        path: 'spu',
        name: 'Spu',
        component: () => import('@/views/Commodity/Spu/index'),
        meta: { title: 'SPU管理', icon: 'el-icon-film' }
      },
      {
        path: 'sku',
        name: 'Sku',
        component: () => import('@/views/Commodity/Sku/index'),
        meta: { title: 'SKU管理', icon: 'el-icon-data-analysis' }
      }
    ]
  }
]

/**
 * 任意路由表
 * 匹配任意地址，跳转到404(想要生效，必须是路由表的最后)
 */
export const anyRoutes = [
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: staticRoutes
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
